package logic

import (
	"context"
	"gozero-microservices/user-service/rpc/internal/svc"
	"gozero-microservices/user-service/rpc/types/userDeliveryAddress"
	"strings"

	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

/*
 +----------------------------------------------------------------------
 + Title        : UserDeliveryAddrLogic
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2025-08-08
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 用户收货地址逻辑
 +----------------------------------------------------------------------
*/

type UserDeliveryAddrLogic struct {
	BaseLogic
}

/**
 * 创建用户收货地址逻辑实例
 *
 * @param context.Context ctx ----------------------必传
 * @param *svc.ServiceContext svcCtx ---------------必传
 * @return *UserDeliveryAddrLogic
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func NewUserDeliveryAddrLogicInstance(ctx context.Context, svcCtx *svc.ServiceContext) *UserDeliveryAddrLogic {
	return &UserDeliveryAddrLogic{
		BaseLogic{
			Ctx:    ctx,
			SvcCtx: svcCtx,
			Logger: logx.WithContext(ctx),
		},
	}
}

func (logic *UserDeliveryAddrLogic) AddUserDeliveryAddr(in *userDeliveryAddress.AddRequest) (*userDeliveryAddress.Result, error) {
	return &userDeliveryAddress.Result{}, nil
}

func (logic *UserDeliveryAddrLogic) DeleteUserDeliveryAddr(in *userDeliveryAddress.DeleteRequest) (*userDeliveryAddress.Result, error) {
	// todo: add your logic here and delete this line

	return &userDeliveryAddress.Result{}, nil
}

func (logic *UserDeliveryAddrLogic) EditUserDeliveryAddr(in *userDeliveryAddress.EditRequest) (*userDeliveryAddress.Result, error) {
	// todo: add your logic here and delete this line

	return &userDeliveryAddress.Result{}, nil
}

func (logic *UserDeliveryAddrLogic) EditUserDeliveryAddrStatus(in *userDeliveryAddress.EditStatusRequest) (*userDeliveryAddress.Result, error) {
	// todo: add your logic here and delete this line

	return &userDeliveryAddress.Result{}, nil
}

/**
 * 详情
 *
 * @param uint64 Id ---------------------------必传,Id
 * @return *userDeliveryAddress.DetailResponse, error
 * @author huwl
 * @date 2025/10/20 18:51:38
 */
func (logic *UserDeliveryAddrLogic) UserDeliveryAddrDetail(in *userDeliveryAddress.DetailRequest) (*userDeliveryAddress.DetailResponse, error) {
	if in.Id <= 0 {
		return nil, status.Errorf(codes.InvalidArgument, "Id错误")
	}

	// 操作数据库
	userDeliveryAddressRes, err := logic.SvcCtx.UserDeliveryAddressModel.FindOne(logic.Ctx, int64(in.Id))
	if err != nil {
		if strings.Contains(err.Error(), "no rows in result set") {
			return nil, status.Errorf(codes.Aborted, "收货地址信息错误")
		}

		return nil, status.Errorf(codes.Aborted, err.Error())
	}
	if userDeliveryAddressRes.Status == -2 {
		return nil, status.Errorf(codes.Aborted, "收货地址信息错误")
	}

	return &userDeliveryAddress.DetailResponse{
		UserDeliveryAddr: &userDeliveryAddress.UserDeliveryAddr{
			Id:         userDeliveryAddressRes.Id,
			Uid:        userDeliveryAddressRes.Uid,
			Name:       userDeliveryAddressRes.Name,
			Mobile:     userDeliveryAddressRes.Mobile,
			ProvinceId: uint32(userDeliveryAddressRes.ProvinceId),
			CityId:     uint32(userDeliveryAddressRes.CityId),
			DistrictId: uint32(userDeliveryAddressRes.DistrictId),
			Region:     userDeliveryAddressRes.Region,
			Address:    userDeliveryAddressRes.Address,
			IsDefault:  uint32(userDeliveryAddressRes.IsDefault),
			Status:     int32(userDeliveryAddressRes.Status),
			CreateTime: userDeliveryAddressRes.CreateTime.String(),
			UpdateTime: userDeliveryAddressRes.UpdateTime.String(),
		},
	}, nil
}

func (logic *UserDeliveryAddrLogic) UserDeliveryAddrList(in *userDeliveryAddress.QueryRequest) (*userDeliveryAddress.ListResponse, error) {
	// todo: add your logic here and delete this line

	return &userDeliveryAddress.ListResponse{}, nil
}
